package com.zhiwei.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhiwei.mapper.ProductsMapper;
import com.zhiwei.pojo.Products;
import com.zhiwei.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductsServiceImpl implements ProductsService{
    @Autowired
    private ProductsMapper productsMapper;

    public Products getProduct(Integer productId) {
        return productsMapper.selectById(productId);
    }

    @Override
    public List<Products> getProductLists() {
        QueryWrapper<Products> productsQueryWrapper = new QueryWrapper<>();
        productsQueryWrapper.eq("delete_flag",false);
        List<Products> products = productsMapper.selectList(productsQueryWrapper);
        return products;
    }
    @Override
    public PageResult getProductList(PageResult pageResult) {
        IPage page = new Page(pageResult.getPageNum(),pageResult.getPageSize());
//        selectPage(分页对象，分页条件)
        QueryWrapper<Products> qw = new QueryWrapper<>();
        qw.like(StringUtils.hasLength(pageResult.getQuery()),"name", pageResult.getQuery())
        .eq("delete_flag",false);
//        System.out.println(pageResult);
        IPage iPage = productsMapper.selectPage(page, qw);
        List<Products> requests = iPage.getRecords();
        PageResult Result = pageResult.setRows(requests).setTotal(iPage.getTotal());
        return Result;
    }

    @Override
    public void updateproducts(Products products) {
        productsMapper.updateById(products);
    }

    @Override
    public void updatestatus(Products products) {
        products.setStatus(!products.getStatus());
        productsMapper.updateById(products);
    }

    @Override
    public void deleteproducts(Integer id) {
        Products products = productsMapper.selectById(id);
        products.setDeleteFlag(true);
        productsMapper.updateById(products);
    }

    @Override
    public void intertproducts(Products products) {
        products.setStatus(false);
        productsMapper.insert(products);
    }
}
